/*
 * main.cpp
 *
 *  Created on: 2011/06/17
 *      Author: naoki
 */

#if 0

#include "Framework.h"
#include "DebugScreen.h"
#include "NTEngine/Base/Compressor.h"
#include "NTEngine/Base/IBinaryStream.h"
#include "NTEngine/Base/PrimeNumber.h"
#include "NTEngine/Core/Core.h"
#include "NTEngine/Graphics/Manager.h"
#include "NTEngine/Graphics/Enum.h"
#include "NTEngine/Graphics/VertexBuffer.h"
#include "NTEngine/Graphics/IndexBuffer.h"
#include "NTEngine/Graphics/Texture.h"
#include "NTEngine/Math/Function.h"
#include "NTEngine/Math/Vector2.h"
#include "NTEngine/Math/Vector3.h"
#include "NTEngine/Math/Vector4.h"
#include "NTEngine/Math/Matrix3x4.h"
#include "NTEngine/Math/Matrix4x4.h"
#include "NTEngine/FileIO/Manager.h"
#include "NTEngine/FileIO/InFile.h"
#include "NTEngine/FileIO/OutFile.h"
#include "NTEngine/Input/Touch.h"
#include "NTEngine/Sound/Wave.h"
#include "NTEngine/Sound/Player.h"
#include "NTEngine/Scene/PrimitiveRenderer.h"
#include "NTEngine/Scene/Container.h"
#include "NTEngine/Scene/Batch.h"
#include "NTEngine/Scene/Model.h"
#include "NTEngine/Xml/Document.h"
#include "NTEngine/Xml/Attribute.h"
#include "NTEngine/Xml/Element.h"

//#define ON_MODEL_DRAW

namespace NTEngine {
using namespace Math;

class Camera {
public:
	Camera()
	: mPostion( 0 ), mTarget( 0 ), mFieldOfView( 0 ), mNearClip( 0 ), mFarClip( 0 )
	{ }
	void setPosition( float x, float y, float z ) {
		mPostion.set( x, y, z );
	}
	void setTarget( float x, float y, float z ) {
		mTarget.set( x, y, z );
	}
	void setFieldOfView( float f ) {
		mFieldOfView = f;
	}
	void setNearClip( float c ) {
		mNearClip = c;
	}
	void setFarClip( float c ) {
		mFarClip = c;
	}

	const Vector3* position() const {
		return &mPostion;
	}

	void getProjectionViewMatrix( Matrix4x4* p ) const {
		Graphics::Manager g;
		p->setPerspectiveTransform( mFieldOfView, g.width(), g.height(), mNearClip, mFarClip );
		Vector3 u( 0, 1.f, 0 );
		p->multiplyViewTransform( mPostion, mTarget, u );
	}
private:
	Vector3 mPostion;
	Vector3 mTarget;
	float mFieldOfView;
	float mNearClip;
	float mFarClip;
};

// グローバル変数
namespace {
Scene::PrimitiveRenderer gPRenderer;
Camera gCamera;
float angle = 0;
unsigned timer = 0;
Graphics::VertexBuffer vertexBuffer;
Graphics::IndexBuffer indexBuffer;
Graphics::Texture texture;
Sound::Wave wave;
Sound::Player player;
Xml::Document doc;
#ifdef ON_MODEL_DRAW
Scene::Container container;
Array< Scene::Batch > batches;
Scene::Model model;
#endif
int sh_type = 0;
Vector2 pos;
Vector2 vel;
Vector2 prv;

int gCount = 0;

struct SaveData {
	int value;
} sSaveData;

static int status = 0;
static void init() {
	DebugScreen ds = DebugScreen::instance();
	//ds.setPosition( 0, 0 );
	// フレームレート出力
	ds << "Now Loading" << endl;

	gCount = ++gCount % 60;
	if ( gCount < 20 ) {
		ds << "." << endl;
	} else if ( gCount < 40 ) {
		ds << ".." << endl;
	} else {
		ds << "..." << endl;
	}
	

	if ( !texture.isReady() ) {
		return;
	}
	if ( !doc.isReady() ) {
		return;
	}
#ifdef ON_MODEL_DRAW
	if ( !container.isReady() ) {
		return;
	}
#endif
	if ( wave.isReady() ) {
		ASSERT( !wave.isError() );
		if ( !player ) {
			player = Sound::Player::create( wave );
		}
		player.play();
		ASSERT( !doc.isError() );
		vertexBuffer = Graphics::VertexBuffer::create( doc.child( 0 ) );

#ifdef ON_MODEL_DRAW
		// batch
		batches.setSize( container.batchNumber() );
		for ( int i = 0; i < batches.size(); ++i ) {
			batches[ i ] = container.batch( i );
		}
		//model = container.createModel( "batch0_0" );
#endif
		pos.zero();
		vel.zero();
		prv.zero();
		status = 1;
	}
}

static void exec() {
	Core c = Core::instance();
	Framework f = Framework::instance();
	Graphics::Manager g = Graphics::Manager::instance();
	// ステータスバーがある場合rowsは3ぐらいに設定する
	DebugScreen ds = DebugScreen::instance();
	//ds.setPosition( 0, 0 );
	// フレームレート出力
	ds << "FPS:" << f.frameRate() << endl;
	ds << "Elapse:" << f.elapsedTime() << endl;
	//ds << "TEST" << endl;
	Input::Touch touch;
	for ( int i = 0; i < 4; ++i ) {
		ds << "Touch[" << i << "]X:" << touch.x( i ) << ",Y:" << touch.y( i ) << ",Size:" << touch.size( i ) <<
			",isOn:" << touch.isOn( i ) << ",isTriggered:" << touch.isTriggered( i ) <<
		endl;
	}
	ds << "SaveData.value:" << sSaveData.value << endl;

	float fct = static_cast< float >( f.elapsedTime() ) / 16.6f; // 60fps想定
	fct *= 0.005f;
	angle += fct;
	if ( angle > Math::PI ) {
		angle = 0;
	}
	timer += 1;
	if ( timer % 600 == 0 ) {
		sh_type = 1 - sh_type;
	}

	g.setCullMode( Graphics::CULL_BACK );
	g.setBlendMode( Graphics::BLEND_LINEAR );

	g.enableDepthTest( true );
	g.enableDepthWrite( true );

	// バッファをセット
	g.setVertexBuffer( vertexBuffer );
	g.setIndexBuffer( indexBuffer );
	g.setTexture( texture );

	// ワールド変換行列作成
	Matrix3x4 w;
	w.setRotationY( angle );
	w.rotateX( angle );
	w.rotateZ( angle );
//	w.scale( 3.f );
	// プロジェクション×ビュー変換行列作成
	Matrix4x4 pv;
	gCamera.getProjectionViewMatrix( &pv );

	// 行列セット
	g.setProjectionViewMatrix( pv );
	g.setWorldMatrix( w );
	// ライトパラメータセット
	g.setEyePosition( *(gCamera.position()) );
	g.setLightColor( 0, Math::Vector3( 1.f, 1.f, 1.f ) );
	g.setLightPosition( 0, Math::Vector3( 10.f, 10.f, 10.f ) );
	g.setLightIntensity( 0, 17.2f ); // ルート3 * 10
	g.setDiffuseColor( Math::Vector3( 1.0f, 1.0f, 1.0f ) );
	g.setTransparency( 1.f );
	g.setAmbientColor( Math::Vector3( 0.3f, 0.3f, 0.3f ) );
	g.setEmissionColor( Math::Vector3( 0.6f, 0.6f, 0.6f ) );
	g.setSpecularColor( Math::Vector3( 1.f, 1.f, 1.f ) );
	g.setSpecularSharpness( 1000.f );

	// テクスチャフィルタ指定
	//gm.setTextureFilter( Graphics::TEXTURE_FILTER_POINT );
	g.setTextureFilter( Graphics::TEXTURE_FILTER_LINEAR );

	// 描画
	g.setLightingMode( Graphics::LIGHTING_PER_PIXEL );
	g.setCullMode( Graphics::CULL_BACK );
	g.draw( 0, 12, Graphics::PRIMITIVE_TRIANGLE );

#ifdef ON_MODEL_DRAW
	w.setRotationY( angle );
	g.setWorldMatrix( w );
	for ( int i = 0; i < batches.size(); ++i ) {
		batches[ i ].draw();
	}
	//model.draw();
#endif

	// 他
	if ( touch.isTriggered() ) {
		prv.set( touch.x(), touch.y() );
	} else {
		if ( touch.isOn() ) {
			Vector2 tmp( touch.x(), touch.y() );
			vel.setSub( tmp, prv );
			prv = tmp;
		} else {
			vel *= 0.9f;
			if ( vel.length() < 0.1f ) {
				vel.zero();
			}
		}
		pos += vel;
	}
	ds << "px:" << pos.x << ",py:" << pos.y << endl;

	gPRenderer.setCullMode( Graphics::CULL_NONE );
	gPRenderer.setBlendMode( Graphics::BLEND_LINEAR );
	gPRenderer.setTexture( texture );
	gPRenderer.addRectangle(
		pos, Vector2( pos.x + 100.f, pos.y + 100.f ),
		Vector2( 0.f, 0.f ), Vector2( 1.f, 1.f )
	);
	gPRenderer.draw();
}

typedef void (*FUNC)();
FUNC sFunc[] ={
	init,
	exec
};
}

void Framework::configure( Configuration config ) {
	cout << "configure" << endl;
	// TODO:エンジンの初期化に必要なコンフィグの設定など
	config.enableDepthStencil( true ); // 3Dでは必須
	config.enableAntialias( false ); // すげー重い
	config.enableAnisotropicFilter( false ); // サポート端末が少ない
	config.translucent( true ); // レンダーバッファをリッチにしたいなら・・・あまり速度に影響しない？

	// TODO:リンク関連
	IBinaryStream ibs( "" );
	PrimeNumber::next( 1 );
	Compressor();
}

void Framework::initialize() {
	cout << "initialize" << endl;
	// TODO:アプリケーションの初期化処理
	// グラフィックはまだ初期化されていない可能性があるので
	// ここではなくonResume側に作成すると良い
	gCamera.setFarClip( 100.f );
	gCamera.setNearClip( 1.f );
	gCamera.setFieldOfView( 0.25f );
	gCamera.setPosition( 0, 1.f, 3.f );
	gCamera.setTarget( 0, 0, 0 );

	// wave
	wave = Sound::Wave::create( "/sdcard/ntengine/th12_6b.wav" );

	doc = Xml::Document::create( "/sdcard/ntengine/cube.txt" );

	// runステート
	status = 0;
}

void Framework::update() {
	// TODO:メインループ
	sFunc[ status ]();
}

void Framework::terminate() {
	cout << "terminate" << endl;
	// TODO:アプリケーションで使うシングルトンなどのグローバルなものを開放する
	indexBuffer.release();
	vertexBuffer.release();
	texture.release();

	gPRenderer.release();

	player.release();
	wave.release();

#ifdef ON_MODEL_DRAW
	for ( int i = 0; i < batches.size(); ++i ) {
		batches[ i ].release();
	}
	batches.clear();
//	model.release();
	container.release();
#endif

	doc.release();
}

#ifdef ANDROID
void Framework::onResume() {
	cout << "onResume" << endl;
	// TODO:アプリケーションの復帰時に行う処理

	// テクスチャ
	texture = Graphics::Texture::create( "/sdcard/ntengine/bar.dds" );

	// プリミティブレンダラ
	gPRenderer = Scene::PrimitiveRenderer::create( 1000, 100 );

#ifdef ON_MODEL_DRAW
	container = Scene::Container::create( "/sdcard/ntengine/hoshi.txt", "/sdcard/ntengine" );
#endif

	if ( FileIO::Manager().isExists( "/sdcard/ntengine/sample.txt" ) ) {
		FileIO::InFile fi = FileIO::InFile::create( "/sdcard/ntengine/sample.txt" );
		while ( !fi.isFinished() ) { }
		sSaveData.value = fi.getInt( 0 );
	} else {
		sSaveData.value = 0;
	}

	status = 0;
}

void Framework::onPause() {
	cout << "onPause" << endl;
	// TODO:アプリケーションの一時停止時に行う処理
	texture.release();
	indexBuffer.release();
	vertexBuffer.release();

	gPRenderer.release();

	player.pause();

#ifdef ON_MODEL_DRAW
	for ( int i = 0; i < batches.size(); ++i ) {
		batches[ i ].release();
	}
	batches.clear();
//	model.release();
	container.release();
#endif

	sSaveData.value += 1;
	FileIO::OutFile fo = FileIO::OutFile::create( "/sdcard/ntengine/sample.txt",
			(const char*)&sSaveData, sizeof( SaveData ) );
	while ( !fo.isFinished() ) { }
}
#endif

} // namespace NTEngine

#else
#include "Framework.h"
#include "DebugScreen.h"
#include "NTEngine/Base/Compressor.h"
#include "NTEngine/Base/IBinaryStream.h"
#include "NTEngine/Base/PrimeNumber.h"
#include "NTEngine/Math/Matrix4x4.h"
#include "NTEngine/Math/Matrix3x4.h"
#include "NTEngine/Math/Vector4.h"
#include "NTEngine/Math/Vector3.h"
#include "NTEngine/Math/Vector2.h"
#include "NTEngine/Math/Function.h"
#include "NTEngine/Scene/PrimitiveRenderer.h"
#include "NTEngine/Graphics/Manager.h"
#include "NTEngine/Input/Manager.h"
#include "NTEngine/Input/Keyboard.h"
#include "NTEngine/Input/Touch.h"
#include "NTEngine/Graphics/IndexBuffer.h"
#include "NTEngine/Graphics/VertexBuffer.h"
#include "NTEngine/Graphics/Texture.h"

#include "NTEngine/Sound/Wave.h"
#include "NTEngine/Sound/Player.h"
using namespace NTEngine;

#include <limits>
using namespace std;

#include "Pad.h"
#include "Robo.h"
#include "Stage.h"

Robo* gRobo[ 2 ];
Stage* gStage;
int gTime;
bool gPlaying = false;
const int TIME_LIMIT = 90; //90秒

Vector3 gAmbient( 0.0f, 0.2f, 0.4f );
Scene::PrimitiveRenderer gPrimitiveRenderer;

Sound::Wave gWave;
Sound::Player gPlayer;

void drawRect( Vector2* p, unsigned c1, unsigned c2 ){
	Framework f = Framework::instance();
	Vector4 p4[ 4 ];
	for ( int i = 0; i < 4; ++i ){
		p4[ i ].x = p[ i ].x;
		p4[ i ].y = p[ i ].y;
		p4[ i ].z = 0.0; //zは0固定
		p4[ i ].w = 1.0; //wは1固定
	}
	gPrimitiveRenderer.addTransformedTriangle( p4[ 0 ], p4[ 1 ], p4[ 2 ], c1, c1, c2 );
	gPrimitiveRenderer.addTransformedTriangle( p4[ 3 ], p4[ 1 ], p4[ 2 ], c2, c1, c2 );
}


//ユーザ実装関数。中身はmainLoop()に
namespace NTEngine {

namespace { // anonymous
static const char* ARCHIVE_FILES[] ={
	"/sdcard/ntengine/data.bin",
	//"/sdcard/ntengine/ia.bin",
	"/sdcard/ntengine/IA2.bin",
	//"/sdcard/ntengine/haku.bin",
};
} // namespace {}

void Framework::configure( Configuration c ) {
	// アーカイブ設定
	const int archiveNumber = sizeof( ARCHIVE_FILES ) / sizeof( ARCHIVE_FILES[ 0 ] );
	c.setArchiveNumber( archiveNumber );
	for ( int i = 0; i < archiveNumber; ++i ) {
		c.setArchiveName( i, ARCHIVE_FILES[ i ] );
	}
#ifndef NDEBUG
	c.setLoadMode( LOAD_ARCHIVE_FIRST );
#else
	c.setLoadMode( LOAD_ARCHIVE_ONLY );
#endif
	// TODO:リンク関連
	IBinaryStream ibs( "" );
	PrimeNumber::next( 1 );
	Compressor();
}
void Framework::initialize() {
	//gWave = Sound::Wave::create( "/sdcard/ntengine/th12_6b.wav" );
	gWave = Sound::Wave::create( "/sdcard/ntengine/sample001.wav" );
}
void Framework::update() {
	if ( !gPlayer ) {
		if ( gWave.isReady() ) {
			ASSERT( !gWave.isError() );
			gPlayer = Sound::Player::create( gWave );
		}
	}
	if ( !gStage ){
		gPrimitiveRenderer = Scene::PrimitiveRenderer::create( 1000, 100 );
		Pad::create();
		gRobo[ 0 ] = new Robo( 0 );
		gRobo[ 1 ] = new Robo( 1 );
		gRobo[ 0 ]->setPosition( Vector3( 0.0, 0.0, 50.0 ) );
		gRobo[ 0 ]->setAngleY( Math::PI );
		gStage = new Stage();
		gPlaying = true;
	}
	if ( gPlaying ){
		gRobo[ 0 ]->update( gRobo[ 1 ] );
		gRobo[ 1 ]->update( gRobo[ 0 ] );
		++gTime;
		if ( gPlayer && !gPlayer.isPlaying() ) {
			gPlayer.setLoop( Sound::Player::LOOP_INFINITY );
			gPlayer.play();
		}
	}

	DebugScreen ds = DebugScreen::instance();
	ds << "FPS:" << frameRate() << endl;
	ds << "Elapse:" << elapsedTime() << endl;
	//ds << "TEST" << endl;
	Input::Touch touch;
	for ( int i = 0; i < 4; ++i ) {
		ds << "Touch[" << i << "]X:" << touch.x( i ) << ",Y:" << touch.y( i ) << ",Size:" << touch.size( i ) <<
			",isOn:" << touch.isOn( i ) << ",isTriggered:" << touch.isTriggered( i ) <<
		endl;
	}

	Graphics::Manager gm = Graphics::Manager::instance();
	//0番ロボからカメラ行列ゲット
	Matrix4x4 pvm;
	pvm.setPerspectiveTransform( 0.25f * Math::PI,
		static_cast< float >( gm.width() ),
		static_cast< float >( gm.height() ),
		1.f,
		1000.f );
	Matrix3x4 vm;
	Vector3 eyePosition;
	gRobo[ 0 ]->getView( &vm, &eyePosition ); //ビュー行列はロボに作ってもらう
	pvm *= vm;

	//描画
	//グローバルなライティング設定
	gm.setProjectionViewMatrix( pvm );
	gm.setEyePosition( eyePosition );
	gm.setLightingMode( Graphics::LIGHTING_PER_PIXEL );
	gm.setAmbientColor( gAmbient );
	gm.setLightColor( 0, Vector3( 1.0f, 0.8f, 0.6f ) );
	gm.enableDepthTest( true );
	gm.enableDepthWrite( true );
	Vector3 lightPos[ 4 ];
	lightPos[ 0 ].set( 1000.f, 1000.f, 1000.f );
	float lightIntensities[ 4 ];
	lightIntensities[ 0 ] = lightPos[ 0 ].length(); //長さを強度にすれば、だいたいその距離で1になる
	//残り三つのライトは弾のところに置こうか。
	//カメラに近い順に3つ選ぼう。
	float nearestDistance[ 3 ];
	int nearestIndex[ 3 ];
	for ( int i = 0; i < 3; ++i ){
		nearestDistance[ i ] = numeric_limits< float >::max();
		nearestIndex[ i ] = -1;
	}
	//距離を測りながら最小を三つ取得
	//100はロボあたりの弾の数の最大数。
	for ( int i = 0; i < 2; ++i ){
		for ( int j = 0; j < 100; ++j ){
			const Vector3* p = gRobo[ i ]->getBulletPosition( j );
			if ( p ){
				Vector3 t;
				t.setSub( *p, eyePosition );
				float d = t.squareLength();
				if ( d < nearestDistance[ 0 ] ){
					nearestDistance[ 2 ] = nearestDistance[ 1 ];
					nearestDistance[ 1 ] = nearestDistance[ 0 ];
					nearestDistance[ 0 ] = d;
					nearestIndex[ 2 ] = nearestIndex[ 1 ];
					nearestIndex[ 1 ] = nearestIndex[ 0 ];
					nearestIndex[ 0 ] = i * 100 + j;
				}else if ( d < nearestDistance[ 1 ] ){
					nearestDistance[ 2 ] = nearestDistance[ 1 ];
					nearestDistance[ 1 ] = d;
					nearestIndex[ 2 ] = nearestIndex[ 1 ];
					nearestIndex[ 1 ] = i * 100 + j;
				}else if ( d < nearestDistance[ 2 ] ){
					nearestDistance[ 2 ] = d;
					nearestIndex[ 2 ] = i * 100 + j;
				}
			}
		}
	}
	for ( int i = 0; i < 3; ++i ){
		if ( nearestIndex[ i ] != -1 ){
			int robo = nearestIndex[ i ] / 100;
			int bullet = nearestIndex[ i ] % 100;
			const Vector3* p = gRobo[ robo ]->getBulletPosition( bullet );
			lightPos[ i + 1 ] = *p;
			if ( robo == 1 ){
				gm.setLightColor( i + 1, Vector3( 1.f, 0.2f, 0.4f ) );
			}else{
				gm.setLightColor( i + 1, Vector3( 0.2f, 0.4f, 1.f ) );
			}
			lightIntensities[ i + 1 ] = 10.f;
		}else{
			lightIntensities[ i + 1 ] = 0.f;
		}
	}
	gm.setCullMode( Graphics::CULL_BACK );
	//ライト設定
	for ( int i = 0; i < 4; ++i ){
		gm.setLightPosition( i, lightPos[ i ] );
		gm.setLightIntensity( i, lightIntensities[ i ] );
	}
	gStage->draw();
	gRobo[ 0 ]->draw();
	gRobo[ 1 ]->draw();

	//以下フロントエンド描画
	//モデル描x画でいじられたものを元に戻す
	gm.setDiffuseColor( Vector3( 1.f, 1.f, 1.f ) );
	gm.setTransparency( 1.f );
	gm.setLightingMode( Graphics::LIGHTING_NONE );

	//必要な情報を抜いて
	int hp0 = gRobo[ 0 ]->getHitPoint();
	int hp1 = gRobo[ 1 ]->getHitPoint();
	int e0 = gRobo[ 0 ]->getEnergy();
	bool lockOn0 = gRobo[ 0 ]->getLockOn();

	//Zテスト無用。アルファブレンドON
	gPrimitiveRenderer.enableDepthTest( false );
	gPrimitiveRenderer.enableDepthWrite( false );
	gPrimitiveRenderer.setBlendMode( Graphics::BLEND_LINEAR );

	Vector2 p[ 4 ];
	unsigned c1;
	unsigned c2;
	//体力バー背景
	p[ 0 ].set( -0.9f, 0.95f );
	p[ 1 ].set( -0.9f, 0.87f );
	p[ 2 ].set( 0.1f, 0.95f );
	p[ 3 ].set( 0.1f, 0.87f );
	c1 = c2 = 0xff406080;
	drawRect( p, c1, c2 );
	p[ 0 ].set( -0.9f, 0.85f );
	p[ 1 ].set( -0.9f, 0.82f );
	p[ 2 ].set( 0.1f, 0.85f );
	p[ 3 ].set( 0.1f, 0.82f );
	c1 = c2 = 0xff806040;
	drawRect( p, c1, c2 );
	//体力バー本体
	float length = static_cast< float >( hp0 ) / static_cast< float >( Robo::mMaxHitPoint );
	p[ 0 ].set( -0.9f, 0.95f );
	p[ 1 ].set( -0.9f, 0.87f );
	p[ 2 ].set( -0.9f + length, 0.95f );
	p[ 3 ].set( -0.9f + length, 0.87f );
	c1 = 0xff882244;
	c2 = 0xff88ccff;
	drawRect( p, c1, c2 );
	length = static_cast< float >( hp1 ) / static_cast< float >( Robo::mMaxHitPoint );
	p[ 0 ].set( -0.9f, 0.85f );
	p[ 1 ].set( -0.9f, 0.82f );
	p[ 2 ].set( -0.9f + length, 0.85f );
	p[ 3 ].set( -0.9f + length, 0.82f );
	c1 = 0xffff4422;
	c2 = 0xffffcc88;
	drawRect( p, c1, c2 );
	//武器エネルギー
	//背景
	p[ 0 ].set( -0.1f, -0.7f );
	p[ 1 ].set( -0.1f, -0.8f );
	p[ 2 ].set( 0.1f, -0.7f );
	p[ 3 ].set( 0.1f, -0.8f );
	c1 = c2 = 0x80404040;
	drawRect( p, c1, c2 );
	//本体
	gPrimitiveRenderer.setBlendMode( Graphics::BLEND_ADDITIVE ); //加算にしてみようかな
	length = 0.2f * static_cast< float >( e0 ) / static_cast< float >( Robo::mMaxEnergy );
	p[ 0 ].set( -0.1f, -0.7f );
	p[ 1 ].set( -0.1f, -0.8f );
	p[ 2 ].set( -0.1f + length, -0.7f );
	p[ 3 ].set( -0.1f + length, -0.8f );
	c1 = 0x80ff0000;
	c2 = 0x80ffff00;
	drawRect( p, c1, c2 );

	//レーダー
	gPrimitiveRenderer.setBlendMode( Graphics::BLEND_LINEAR );
	//背景
	p[ 0 ].set( 0.7f, 0.7f );
	p[ 1 ].set( 0.7f, 0.9f );
	p[ 2 ].set( 0.9f, 0.7f );
	p[ 3 ].set( 0.9f, 0.9f );
	c1 = c2 = 0x80404040;
	drawRect( p, c1, c2 );
	gPrimitiveRenderer.setBlendMode( Graphics::BLEND_ADDITIVE ); //加算にしてみようか
	//自分
	Vector2 t;
	t.set( gRobo[ 0 ]->position()->x, gRobo[ 0 ]->position()->z ); //x.z
	t *= 0.002f; //+-50のステージを0.2の大きさに縮小するんだから、0.2/100で0.002
	t += Vector2( 0.8f, 0.8f ); //(0.8,0.8)が中心なのだから、それを足す
	p[ 0 ].set( t.x - 0.005f, t.y - 0.005f );
	p[ 1 ].set( t.x - 0.005f, t.y + 0.005f );
	p[ 2 ].set( t.x + 0.005f, t.y - 0.005f );
	p[ 3 ].set( t.x + 0.005f, t.y + 0.005f );
	c1 = c2 = 0xcc0080ff;
	drawRect( p, c1, c2 );
	//敵
	t.set( gRobo[ 1 ]->position()->x, gRobo[ 1 ]->position()->z ); //x.z
	t *= 0.002f; //+-50のステージを0.2の大きさに縮小するんだから、0.2/100で0.002
	t += Vector2( 0.8f, 0.8f ); //(0.8,0.8)が中心なのだから、それを足す
	p[ 0 ].set( t.x - 0.005f, t.y - 0.005f );
	p[ 1 ].set( t.x - 0.005f, t.y + 0.005f );
	p[ 2 ].set( t.x + 0.005f, t.y - 0.005f );
	p[ 3 ].set( t.x + 0.005f, t.y + 0.005f );
	c1 = c2 = 0xccff8000;
	drawRect( p, c1, c2 );

	//ロックオンマーク
	if ( lockOn0 ){
		gPrimitiveRenderer.setBlendMode( Graphics::BLEND_ADDITIVE ); //加算にしてみようかな
		//敵ロボを座標変換してスクリーン座標に変換
		Vector3 t = *gRobo[ 1 ]->position();
		Vector4 p4;
		pvm.multiply( t, &p4 );
		//XY座標はwで割れば出る。
		float x = p4[ 0 ] / p4[ 3 ];
		float y = p4[ 1 ] / p4[ 3 ];
		//色は赤かなあ
		c1 = c2 = 0x80ff0000;
		//線4本でやろう。
		p[ 0 ].set( x - 0.01f, y + 0.2f );
		p[ 1 ].set( x - 0.01f, y + 0.1f );
		p[ 2 ].set( x + 0.01f, y + 0.2f );
		p[ 3 ].set( x + 0.01f, y + 0.1f );
		drawRect( p, c1, c2 );
		p[ 0 ].set( x - 0.01f, y - 0.2f );
		p[ 1 ].set( x - 0.01f, y - 0.1f );
		p[ 2 ].set( x + 0.01f, y - 0.2f );
		p[ 3 ].set( x + 0.01f, y - 0.1f );
		drawRect( p, c1, c2 );
		p[ 0 ].set( x - 0.2f, y - 0.01f );
		p[ 1 ].set( x - 0.2f, y + 0.01f );
		p[ 2 ].set( x - 0.1f, y - 0.01f );
		p[ 3 ].set( x - 0.1f, y + 0.01f );
		drawRect( p, c1, c2 );
		p[ 0 ].set( x + 0.2f, y - 0.01f );
		p[ 1 ].set( x + 0.2f, y + 0.01f );
		p[ 2 ].set( x + 0.1f, y - 0.01f );
		p[ 3 ].set( x + 0.1f, y + 0.01f );
		drawRect( p, c1, c2 );
	}
	//時間制限
	gPrimitiveRenderer.setBlendMode( Graphics::BLEND_LINEAR ); //もどして
	length = 1.9f * static_cast< float >( 60 * TIME_LIMIT - gTime ) / static_cast< float >( 60 * TIME_LIMIT );
	p[ 0 ].set( -0.95f, -0.95f );
	p[ 1 ].set( -0.95f, -0.9f );
	p[ 2 ].set( 0.95f, -0.95f );
	p[ 3 ].set( 0.95f, -0.9f );
	c1 = c2 = 0x80404040;
	drawRect( p, c1, c2 );
	p[ 0 ].set( -0.95f, -0.95f );
	p[ 1 ].set( -0.95f, -0.9f );
	p[ 2 ].set( -0.95f + length, -0.95f );
	p[ 3 ].set( -0.95f + length, -0.9f );
	gPrimitiveRenderer.setBlendMode( Graphics::BLEND_ADDITIVE ); //加算?
	c1 = 0x80ff8888;
	c2 = 0x8088ffff;
	drawRect( p, c1, c2 );

	// Pad

	//if ( Input::Manager().keyboard().isTriggered( ' ' ) ){
	if ( Input::Keyboard().isTriggered( ' ' ) ) {
		gPlaying = !gPlaying;
	}

	if ( hp0 <= 0 || hp1 <= 0 || gTime >= 60 * TIME_LIMIT ){
		gPlaying = false;
		DebugScreen::instance().draw( 20, 5, "Game Over. press Fire" );
		gPlayer.stop();
		if ( Pad::instance()->isTriggered( Pad::FIRE, 0 ) ){
			//セットしているものは後始末
			gm.setVertexBuffer( 0 );
			gm.setIndexBuffer( 0 );
			gm.setTexture( 0 );

			SAFE_DELETE( gRobo[ 0 ] );
			SAFE_DELETE( gRobo[ 1 ] );
			gRobo[ 0 ] = new Robo( 0 );
			gRobo[ 1 ] = new Robo( 1 );
			gRobo[ 0 ]->setPosition( Vector3( 0.f, 0.f, 50.f ) );
			gRobo[ 0 ]->setAngleY( Math::PI );
			gPlaying = true;
			gTime = 0;
		}
	}
	gPrimitiveRenderer.draw(); //足し終わったら描画
}


void Framework::terminate() {
	//終了判定(マウスで×ボタンが叩かれたか)
	if ( gStage ){
		Pad::destroy();
		SAFE_DELETE( gRobo[ 0 ] );
		SAFE_DELETE( gRobo[ 1 ] );
		SAFE_DELETE( gStage );
		//グローバルにあるものは勝手には消えないので開放
		gPrimitiveRenderer.release();
	}
	gPlayer.release();
	gWave.release();
}

#ifdef ANDROID
void Framework::onResume() {
}
void Framework::onPause() {
	//終了判定(マウスで×ボタンが叩かれたか)
	if ( gStage ){
		Pad::destroy();
		SAFE_DELETE( gRobo[ 0 ] );
		SAFE_DELETE( gRobo[ 1 ] );
		SAFE_DELETE( gStage );
		//グローバルにあるものは勝手には消えないので開放
		gPrimitiveRenderer.release();
	}
}
#endif // ANDROID
} // namespace NTEngie

#endif // if 0
